﻿using System;
using System.Collections.Generic;
using System.Data;
using UDS.Entity;

namespace UDS.Components
{
    /// <summary>
    /// 对权限的操作
    /// </summary>
    public static class AssignRights
    {
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="procId">权限代号</param>
        /// <param name="basedOn">基于对象</param>
        /// <param name="objId">权限范围</param>
        /// <param name="srcId">拥有权限的对象集合</param>
        public static void AddRight(long srcId, long objId, EntityType basedOn, int procId)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@Act_ID", SqlDbType.Int, 4, procId),
                Database.MakeInParam("@Based_On", SqlDbType.Int, 4, (int)basedOn),
                Database.MakeInParam("@Src_ID", SqlDbType.Int, 4, srcId),
                Database.MakeInParam("@Obj_ID", SqlDbType.Int, 4, objId)
            };

            using (var db = new Database())
            {
                db.RunProc("dbo.sproc_Rights_AddAssignRule_V2", parameters);
            }
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="ruleIds">权限记录号</param>
        public static void DeleteRight(IEnumerable<int> ruleIds)
        {
            /*
             * XML Format
             * <rules>
             *  <r id="" />
             * </rules>
             */
            string xml = Utility.GenerateXML(ruleIds, "rules", "r", "id");
            var parameters = new[]
            {
                Database.MakeInParam("@RuleIDXml", SqlDbType.Xml, -1, xml)
            };

            using (var db = new Database())
            {
                db.RunProc("dbo.sproc_Rights_DeleteAssignRules_V2", parameters);
            }
        }

        /// <summary>
        /// 获得权限列表
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="ClassID">权限对象</param>
        /// <returns>整形是否成功</returns>
        public static IList<AssignRight> GetRightList(string userName, int ClassID)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@UserName", SqlDbType.NVarChar, 50, userName),
                Database.MakeInParam("@ClassID", SqlDbType.Int, 4, ClassID)
            };

            using (var db = new Database())
            {
                using (var dr = db.RunDataReader("dbo.sproc_Rights_GetRightsToClass_V2", parameters))
                {
                    List<AssignRight> list = new List<AssignRight>();
                    while (dr.Read())
                    {
                        list.Add(AssignRight.ReadAssignRight(dr));
                    }

                    return list;
                }
            }
        }

        /// <summary>
        /// 判断用户对对象是否有某个权限
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="classId">被检查的对象</param>		
        /// <param name="permissionType">权限ID</param>
        /// <returns>是否有权限</returns>
        public static bool IsAccessAllowed(string userName, int classId, PermissionType permissionType)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@Class_ID", SqlDbType.Int, 4, classId),
                Database.MakeInParam("@UserName", SqlDbType.NVarChar, 50, userName),
                Database.MakeInParam("@Act_ID", SqlDbType.Int, 4, (int)permissionType),
                Database.MakeOutParam("@ReturnValue", SqlDbType.Int, 4)
            };

            using (var db = new Database())
            {
                db.RunProc("dbo.sproc_Rights_GetAccessPermission_V2", parameters);
            }
            return (int)parameters[3].Value == 1;
        }
    }
}